#Import Modules
import os, pygame, math, random
from pygame.locals import *
from Game import *

from Projectile import Projectile

# Load up our helper functions
from Loaders import *

class Ball(Projectile):
    """this object contains the ball character"""
    def __init__(self, spawnpoint):
        Projectile.__init__(self, spawnpoint,(True,True)) #call Projectile initializer
        
        # Load the initial image
        self.image, self.rect = load_image(os.path.join('data','characters','ball',"ball.png"))
        
        # Setup the object padding
        self.padx = -20
        self.pady = -20
        
        # Non-default physical properties
        self.friction = 0.1
        self.elasticity = 0.8
        self.gravity = -0.2
        self.accelerationy = self.gravity # Since the ball is not a character, we need to set its gravity manually 
        self.health = 50
        self.invincibleframes = 25
        #self.maxhealth = 50
        
        # Properties for animation, AI, etc...
        # These variables are not referenced outside the class. They are what you make of them.
        self.spawnpoint = spawnpoint
        self.scale = 1
        
    def update(self):
        # This method runs once per frame. Use it for control/AI, animation, and sound effects 
        
        Projectile.update(self)
        
        if self.dying == 1 and self.scale > 0: # If we are dying, shrink the ball to nothing, then set the dead flag.
            self.image = pygame.Surface((self.rect.width,self.rect.height),SRCALPHA)
            self.image.blit(pygame.transform.scale(self.original,(int(self.rect.width*self.scale),int(self.rect.height*self.scale))),((self.rect.width - self.rect.width*self.scale)/2,(self.rect.height - self.rect.height*self.scale)))
            self.scale = self.scale - 0.05
        if self.scale <= 0.05:
            self.dead = 1

class Debris(Projectile):
    """these a shards of the balloon"""
    firstinit = True
    image = None
    rect = None
    
    def __init__(self, spawnpoint, power):
        Projectile.__init__(self, spawnpoint,(False,False)) #call Projectile initializer
        
        if Debris.firstinit:
            Debris.firstinit = False
            Debris.image, Debris.rect = load_image(os.path.join('data','characters','ball',"debris.png"))

        self.image, self.rect = Debris.image, Rect(Debris.rect)
        
        self.power = power * random.random()
        self.orientation = random.randint(0,360)
        
        #self.padx = -110
        #self.pady = -110
        self.health = 1000
        
        self.image = pygame.transform.rotate(self.image,self.orientation)
        self.friction = 0
        self.speedx = self.power * math.cos(self.orientation)
        self.speedy = -self.power * math.sin(self.orientation)
        
    # Override the hit function so that the projectile doesn't get hurt or deflected.
    def hit(self, damage, stun=20, direction=-1, knockback=10): pass
    
    def update(self):
        # This method runs once per frame. Use it for control/AI, animation, and sound effects 
        Projectile.update(self)
        if self.dying == 1:
            self.dead = 1
            
class ExplodingBalloon(Projectile):
    """this projectile is an exploding balloon that hurts friend and foe"""
    firstinit = True
    frames = list()
    image = None
    rect = None
    sfx = list()
    
    def __init__(self, spawnpoint):
        Projectile.__init__(self, spawnpoint,(True,True)) #call Projectile initializer
        
        if ExplodingBalloon.firstinit:
            ExplodingBalloon.firstinit = False
            # Load the initial image
            ExplodingBalloon.image, ExplodingBalloon.rect = load_image(os.path.join('data','characters','ball',"ball.png"))
            ExplodingBalloon.original = ExplodingBalloon.image

            # Load up the explosion animation
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-1.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-2.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-3.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-4.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-5.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-6.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-7.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-8.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-9.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-10.png")))
            ExplodingBalloon.frames.append(load_frame(os.path.join('data','characters','ball',"ball-explode-11.png")))
            
            #Load up SFX
            ExplodingBalloon.sfx.append(load_sound("popexplode.wav"))
        
        self.image, self.rect = ExplodingBalloon.image, Rect(ExplodingBalloon.rect)
        
        # Setup the object padding
        self.padx = -110
        self.pady = -110
        
        # Non-default physical properties
        self.friction = 0.1
        self.elasticity = 0.8
        self.gravity = -0.2
        self.accelerationy = self.gravity # Since the ball is not a character, we need to set its gravity manually 
        
        self.health = 1
        self.invincibleframes = 25
        self.type = COMBAT_HAZARD
        
        self.explode = 0
        self.wait = 0
        self.animspeed = 2
        
        # Properties for animation, AI, etc...
        # These variables are not referenced outside the class. They are what you make of them.
        self.spawnpoint = spawnpoint
        self.scale = 1
        
    def update(self):
         # This method runs once per frame. Use it for control/AI, animation, and sound effects 
        Projectile.update(self)
        if self.dying == 1:
            if self.wait < self.animspeed:
                self.wait += 1
            else:
                self.wait = 0
                
                if self.explode < len(ExplodingBalloon.frames)-1:
                    self.explode += 1
                    
                    if self.explode == 1:
                        ExplodingBalloon.sfx[0].play()
                    if self.explode == 3:
                        self.attackboxes.append((50,Rect(15,15,125,125)))
                        for x in xrange(30):
                            self.spawner.addchild(Debris((50,50), 20), 1)
                    
                    self.image = ExplodingBalloon.frames[self.explode]

                else:
                    self.dead = 1